package com.cgbl.cg.adapter

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cgbl.cg.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.robot.base.model.CourseChaptarBean
import com.robot.base.model.CourseChaptarPeriodBean

class CourseProfileDirectoryAdapter :
    BaseQuickAdapter<CourseChaptarBean, BaseViewHolder>(R.layout.app_item_course_directory) {

    val expandStatus = mutableMapOf<Int, Boolean>()

    override fun convert(holder: BaseViewHolder, item: CourseChaptarBean) {
        val position = holder.adapterPosition
        holder.setText(R.id.tv_name, "${position + 1}  ${item.chapterName}")
        holder.setGone(R.id.v_top_line, position == 0)
        val courseProfileDirectoryListAdapter = CourseProfileDirectoryListAdapter()
        val recyclerView = holder.getView<RecyclerView>(R.id.course_list)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = courseProfileDirectoryListAdapter
        }
        courseProfileDirectoryListAdapter.setNewInstance(item.courseInfoChaptarPeriodVOS as MutableList<CourseChaptarPeriodBean>?)
        holder.itemView.setOnClickListener {
            expandStatus[position] = !(expandStatus[position] ?: false)
            changeExpandLayout(holder)
        }
//        if (position == 0) {
//            //第一个展开
//            recyclerView.visibility = View.VISIBLE
//            holder.setGone(R.id.iv_down, true)
//            return
//        }
        changeExpandLayout(holder)
    }

    private fun changeExpandLayout(holder: BaseViewHolder) {
        val recyclerView = holder.getView<RecyclerView>(R.id.course_list)
        if (expandStatus[holder.adapterPosition] == true) {
            //展开
            recyclerView.visibility = View.VISIBLE
            holder.getView<ImageView>(R.id.iv_down).rotation = 180f
        } else {
            //收缩
            recyclerView.visibility = View.GONE
            holder.getView<ImageView>(R.id.iv_down).rotation = 0f
        }
    }
}